
package com.magicborrow.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.magicborrow.R;
import com.magicborrow.adapter.AddressAdapter;
import com.magicborrow.beans.AddressBean;
import com.magicborrow.utils.AddressHostUtils;
import com.magicborrow.utils.ViewPressedUtil;
import com.magicborrow.views.MsgDialog;

import java.util.ArrayList;

public class AddressManagerActivity extends BaseActivity implements View.OnClickListener {

    private ListView llAddress;
    private TextView tvAddAddress;
    private AddressAdapter addressAdapter;
    private ArrayList<AddressBean> data;
    private int tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_manager);

        llAddress = (ListView) findViewById(R.id.ll_address);
        data = new ArrayList<>();
        addressAdapter = new AddressAdapter(this, data);
        llAddress.setAdapter(addressAdapter);
        tvAddAddress = (TextView) findViewById(R.id.tv_add_address);
        tvAddAddress.setOnTouchListener(ViewPressedUtil.TouchPress);
        tvAddAddress.setOnClickListener(this);
        llAddress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                intent.putExtra("address",addressAdapter.getItem(position));
                setResult(Activity.RESULT_OK,intent);
                finish();

            }
        });
        llAddress.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                MsgDialog dialog = new MsgDialog(AddressManagerActivity.this, "您要删除\n该地址吗", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        data.remove(addressAdapter.getItem(position));
                        addressAdapter.notifyDataSetChanged();
                        AddressHostUtils.saveLocalHost(AddressManagerActivity.this,data);
                    }
                });
                dialog.show();
                return false;
            }
        });

    }



    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        data.clear();
        if (AddressHostUtils.getLocalHost(this) != null)
            data.addAll(AddressHostUtils.getLocalHost(this));
        if (data.size() == 0 && tag <= 0) {
            tag++;
            Intent intent = new Intent(this, AddressActivity.class);
            startActivity(intent);
        } else {
            addressAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add_address:
                //TODO implement
                Intent intent = new Intent(this, AddressActivity.class);
                startActivity(intent);
                break;
        }
    }
}
